package com.kun.portal.webframe.service;

import com.kun.portal.webframe.dao.BuyGoodsDao;
import com.kun.portal.webframe.dao.BuyOrderDao;
import com.kun.portal.webframe.dao.PurchasePlanDao;
import com.kun.portal.webframe.po.BuyGoodsPo;
import com.kun.portal.webframe.po.BuyOrderPo;
import com.kun.portal.webframe.po.PurchasePlanPo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018-07-10.
 */
@Service
public class PurchaseService {
    @Autowired
    private PurchasePlanDao purchasePlanDao;
    @Autowired
    private BuyOrderDao buyOrderDao;
    @Autowired
    private BuyGoodsDao buyGoodsDao;

    public List<PurchasePlanPo> findAllPurchasePlanPo(){
        return  purchasePlanDao.findAll();
    }
    public void savePurchasePlanPo(PurchasePlanPo purchasePlanPo){
        purchasePlanDao.save(purchasePlanPo);
    }
    public void deletePurchasePlanPo(PurchasePlanPo purchasePlanPo){
        purchasePlanDao.delete(purchasePlanPo);
    }
    public  List<PurchasePlanPo> checkPurchasePlanPo(String goodsCode , Date needDate,String userDept ){

        Specification query = new Specification<PurchasePlanPo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<PurchasePlanPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(goodsCode)){
                    pre.add(cb.like(root.get("goodsCode"), "%"+goodsCode+"%"));
                }
                if(StringUtils.isNotBlank(userDept)){
                    pre.add(cb.like(root.get("userDept"), "%"+userDept+"%"));
                }
                if(null != needDate){
                    pre.add(cb.equal(root.get("needDate"), needDate));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return purchasePlanDao.findAll(query);

    }
    public List<PurchasePlanPo> getPurchasePlanPoByState(){
        return purchasePlanDao.findByState("已审核");
    }

    public List<BuyOrderPo> findAllBuyOrderPo(){
        return buyOrderDao.findAll();
    }
    public List<BuyOrderPo> findBuyOrderPo(String buyNum,Date purchaseDate,String auditStatus){
        Specification query = new Specification<BuyOrderPo>() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root<BuyOrderPo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<javax.persistence.criteria.Predicate> pre= new ArrayList<>();
                if(StringUtils.isNotBlank(buyNum)){
                    pre.add(cb.like(root.get("buyNum"), "%"+buyNum+"%"));
                }
                if(StringUtils.isNotBlank(auditStatus)){
                    pre.add(cb.like(root.get("auditStatus"), "%"+auditStatus+"%"));
                }
                if(null != purchaseDate){
                    pre.add(cb.equal(root.get("purchaseDate"), purchaseDate));
                }
                Predicate predicate = cb.and(pre.toArray(new Predicate[pre.size()]));
                return predicate;
            }
        };
        return buyOrderDao.findAll(query);
    }
    public void delectBuyOrderPo(BuyOrderPo buyOrderPo){
        buyOrderDao.delete(buyOrderPo);
    }
    public List<BuyGoodsPo> saveBuyGoodsPos(List<BuyGoodsPo> buyGoodsPos){
        return buyGoodsDao.save(buyGoodsPos);
    }
    public void saveBuyOrderPo(BuyOrderPo buyOrderPo){
        buyOrderDao.save(buyOrderPo);
    }
}
